package com.tiancheng.trade.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.trade.commom.web.model.request.PayWayListQueryReq;
import com.tiancheng.trade.commom.web.model.request.PaymentPayWayQueryRequest;
import com.tiancheng.trade.commom.web.model.response.PaymentPayWayQueryResponse;
import com.tiancheng.trade.payment.mapper.PaymentWayMapper;
import com.tiancheng.trade.payment.model.PaymentWay;
import com.tiancheng.trade.payment.service.PaymentWayService;
import com.tiancheng.trade.payment.util.ObjectConvertUtil;
import com.tiancheng.trade.payment.vo.api.res.PaymentWayResVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付渠道
 * @author su
 *
 */
@Service
public class PaymentWayServiceImpl extends ServiceImpl<PaymentWayMapper,PaymentWay> implements PaymentWayService {

    @Autowired
    PaymentWayMapper paymentWayMapper;

    @Override
    public List<PaymentWay> getByChannelCode(String channelCode){
        QueryWrapper<PaymentWay> conditions = new QueryWrapper<>();
        conditions.eq("payment_channel", channelCode);
        conditions.orderByDesc("sort");
        return paymentWayMapper.selectList(conditions);
    }

    @Override
    public List<PaymentWayResVO> getByChannelCodeConvert(String channelCode){
        List<PaymentWay> list =  getByChannelCode(channelCode);
        List<PaymentWayResVO> listRes =  list.stream().map(obj-> ObjectConvertUtil.convert(obj, PaymentWayResVO.class).get()).collect(Collectors.toList());
        return listRes;
    }

    @Override
    public PaymentWay get(String payChannel, String payWay) {
        QueryWrapper<PaymentWay> conditions = new QueryWrapper<>();
        conditions.eq("payment_channel", payChannel);
        conditions.eq("payment_way", payWay);
        return paymentWayMapper.selectOne(conditions);
    }

    @Override
    public List<PaymentPayWayQueryResponse> queryOrgPayWay(PaymentPayWayQueryRequest payWayQueryRequest) {
        return paymentWayMapper.queryOrgPayWay(payWayQueryRequest);
    }

    @Override
    public List<PaymentPayWayQueryResponse> queryOrgPayWayList(PayWayListQueryReq payWayListQueryReq) {
        return paymentWayMapper.queryOrgPayWayList(payWayListQueryReq);
    }

    @Override
    public void savePayWay(PaymentWay paymentWay) {
        if(null!=paymentWay.getId()){
            paymentWay.setUpdateTime(LocalDateTime.now());
            this.updateById(paymentWay);
        }else{
            paymentWay.setCreateTime(LocalDateTime.now());
            this.save(paymentWay);
        }
    }
}
